package leo.gptBot.config;

/**
 * @author ：Leo
 * @date ：Created in 2024/7/23 10:05
 * @description：
 */
import com.alibaba.fastjson.support.spring.FastJsonRedisSerializer;
import com.alibaba.fastjson.support.spring.GenericFastJsonRedisSerializer;
import dev.langchain4j.data.message.ChatMessage;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import java.util.List;

@Configuration
public class RedisConfig {
    @Value("${spring.data.redis.host}")
    private String redisHost;

    @Value("${spring.data.redis.port}")
    private Integer redisPort;

    @Bean
    public LettuceConnectionFactory redisConnectionFactory() {
        return new LettuceConnectionFactory(new RedisStandaloneConfiguration(redisHost, redisPort));
    }
//    @Bean
//    public RedisConnectionFactory redisConnectionFactory() {
//        return new LettuceConnectionFactory();
//    }

//    @Bean
//    public RedisTemplate<String, List<ChatMessage>> redisTemplate() {
//        RedisTemplate<String, List<ChatMessage>> template = new RedisTemplate<>();
//        template.setConnectionFactory(redisConnectionFactory());
//        GenericFastJsonRedisSerializer fastJsonRedisSerializer = new GenericFastJsonRedisSerializer();
//        template.setDefaultSerializer(fastJsonRedisSerializer);
//
////        template.setKeySerializer(new StringRedisSerializer());
////
////        template.setValueSerializer(new GenericFastJsonRedisSerializer());
//        return template;
//    }
    @Bean
    public RedisTemplate<String,String> redisTemplate() {
        RedisTemplate<String, String> template = new RedisTemplate<>();
        template.setConnectionFactory(redisConnectionFactory());
        GenericFastJsonRedisSerializer fastJsonRedisSerializer = new GenericFastJsonRedisSerializer();
        template.setDefaultSerializer(fastJsonRedisSerializer);

    //        template.setKeySerializer(new StringRedisSerializer());
    //
    //        template.setValueSerializer(new GenericFastJsonRedisSerializer());
        return template;
    }
}
